using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Marlimer.Logical.Comercial;
using Marlimer.Common;
using Marlimer.Comercial.Reportes.Crystals;  

namespace Marlimer.Comercial.Reportes.Formularios
{
    public partial class frm_RepAnalisisDistribucion : Form
    {

        lcom_Modulo lcom_modulo = new lcom_Modulo();
        lcom_Ruta lcom_ruta = new lcom_Ruta();
        lcom_GrupoVenta lcom_grupoventa = new lcom_GrupoVenta();
        lcom_Zona lcom_zona = new lcom_Zona();
        lcom_Transportista lcom_transportista = new lcom_Transportista(); 

        List<InfoParamRpt> paramsrpt = new List<InfoParamRpt>();

        rptEfectividadTransportista RptEfectividadTransportista = new rptEfectividadTransportista();
        rptEfectividadVendedor RptEfectividadVendedor = new rptEfectividadVendedor();
        rptEfectividadTransportistaRuta RptEfectividadTransportistaRuta = new rptEfectividadTransportistaRuta(); 

        public frm_RepAnalisisDistribucion()
        {
            InitializeComponent();
        }

        private void frm_VentasXProductoXVendedor_Load(object sender, EventArgs e)
        {
            dtpFecIni.Value = DateTime.Today;
            dtpFecFin.Value = DateTime.Today;
 
            CargarCombos();
            this.Location = new Point(0, 0);
        }



        private void CargarCombos()
        {
            Util.LlenarDataCombo(lcom_modulo.getDTModulo(), cboModulo, true);
            Util.LlenarDataCombo(lcom_ruta.getDTRuta(), cboRuta, true);
            Util.LlenarDataCombo(lcom_zona.getDTZona(), cboZona, true);
            Util.LlenarDataCombo(lcom_transportista.getDTTransportista(), cboTransportista, true);    
        }

        private void nuevaBusqueda()
        {
            CargarCombos(); 
        }

        private void txtGeneralEntero_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = Util.validaTecla(e.KeyChar, "Entero", ((TextBox)sender).Text, true);
        }

        private void cboGeneral_KeyPress(object sender, KeyPressEventArgs e)
        {
            Util.validaEnter(e.KeyChar);
        }

        private void txtGeneral_Leave(object sender, EventArgs e)
        {
            TextBox txt = (TextBox)sender;
            String value = "";
            int valuenum = -1;
            String nametxt = txt.Name;
            object newval = null;

            if (txt.Text.Trim() != "")
            {
                value = txt.Text;
                valuenum = Convert.ToInt32(Util.ToValor(txt.Text));
            }

            switch (nametxt)
            {
                case "txtCodModulo":
                    {
                        cboModulo.SelectedValue = value;
                        newval = cboModulo.SelectedValue;
                        break;
                    }
                case "txtCodRuta":
                    {
                        cboRuta.SelectedValue = valuenum;
                        newval = cboRuta.SelectedValue;
                        break;
                    }
                case "txtCodZona":
                    {
                        cboZona.SelectedValue = valuenum;
                        newval = cboZona.SelectedValue;
                        break;
                    }
                case "txtCodTransportista":
                    {
                        cboTransportista.SelectedValue = valuenum;
                        newval = cboTransportista.SelectedValue;  
                        break;
                    }
            }
            txt.Text = Convert.ToString(newval);
        }

        private void cboGeneralDT_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cbo = (ComboBox)sender;
            String valor = "";
            String namecbo = cbo.Name;
            //validamos que el indice seleccionado sea mayor a -1
            if (cbo.SelectedIndex > -1 && cbo.SelectedValue.ToString() != "System.Data.DataRowView")
            {
                valor = Convert.ToString(cbo.SelectedValue);
            }

            switch (namecbo)
            {
                case "cboModulo":
                    {
                        txtCodModulo.Text = valor;
                        break;
                    }
                case "cboRuta":
                    {
                        txtCodRuta.Text = valor;
                        break;
                    }
                case "cboZona":
                    {
                        txtCodZona.Text = valor;
                        break;
                    }
                case "cboTransportista":
                    {
                        txtCodTransportista.Text = valor;
                        break; 
                    }
            }
        }

        private void tstComandos_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            int indice = tstComandos.Items.IndexOf(e.ClickedItem);
            switch (indice)
            {
                case 0:     //nuevo
                    {
                        nuevaBusqueda();
                        break;
                    }
                case 1:     //imprimir o mostrar
                    {
                        CargarReporte();
                        break;
                    }
                case 2:
                    {
                        if (rbtEfecTransportista.Checked)
                        {
                            Util.exportReporte(RptEfectividadTransportista);
                        }
                        else if (rbtEfecVendedor.Checked)
                        {
                            Util.exportReporte(RptEfectividadVendedor);
                        }
                        else if (rbtEfecTransportistaRuta.Checked)
                        {
                            Util.exportReporte(RptEfectividadTransportistaRuta);
                        }
                        break;
                    }
            }
        }


        private void txtGeneralSelect_Enter(object sender, EventArgs e)
        {
            Util.selectAllText(((TextBox)sender));
        }

        private void txtCodModulo_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.KeyChar = Convert.ToChar(e.KeyChar.ToString().ToUpper());
            e.Handled = Util.validaTecla(e.KeyChar, "AlfaNumerico", ((TextBox)sender).Text, true);
        }


        private void CargarReporte()
        {
            paramsrpt.Clear();
            paramsrpt.Add(new InfoParamRpt("@dfec_ini", dtpFecIni.Value));
            paramsrpt.Add(new InfoParamRpt("@dfec_fin", dtpFecFin.Value));
            if (rbtEfecVendedor.Checked)
            {
                paramsrpt.Add(new InfoParamRpt("@ccod_zona", Convert.ToInt32(cboZona.SelectedValue)));
                paramsrpt.Add(new InfoParamRpt("@ccod_ruta", Convert.ToInt32(cboRuta.SelectedValue)));
            }
            else if (rbtEfecTransportista.Checked || rbtEfecTransportistaRuta.Checked)
            {
                paramsrpt.Add(new InfoParamRpt("@ccod_trans", Convert.ToInt32(cboTransportista.SelectedValue)));
            }

            try
            {
                //verificar segun tipo de formato    
                if (rbtEfecTransportista.Checked)
                {
                    crpReporte.ReportSource = Util.setRptParams(RptEfectividadTransportista, paramsrpt);
                }
                else if (rbtEfecVendedor.Checked)
                {
                    crpReporte.ReportSource = Util.setRptParams(RptEfectividadVendedor, paramsrpt);
                }
                else if (rbtEfecTransportistaRuta.Checked)
                {
                    crpReporte.ReportSource = Util.setRptParams(RptEfectividadTransportistaRuta, paramsrpt);
                }
                tblExportar.Enabled = true;
            }
            catch (Exception ex)
            {
                tblExportar.Enabled = false;
            }
        }

        private void rbtEfecTransportista_CheckedChanged(object sender, EventArgs e)
        {
            CargarCombos(); 
            //verificar el radio que esta seleccionado
            if (rbtEfecTransportista.Checked)
            {
                txtCodTransportista.Enabled = true;
                cboTransportista.Enabled = true;
                txtCodZona.Enabled = false;
                cboZona.Enabled = false;
                txtCodRuta.Enabled = false;
                cboRuta.Enabled = false; 
            }
            else if (rbtEfecVendedor.Checked)
            {
                txtCodTransportista.Enabled = false;
                cboTransportista.Enabled = false;
                txtCodZona.Enabled = true;
                cboZona.Enabled = true;
                txtCodRuta.Enabled = true;
                cboRuta.Enabled = true;
            }
        }


    }
}